﻿@model List<Magicodes.WeiChat.Data.Models.Site.Site_Menu>
@{
    var controller = Request.RequestContext.RouteData.Values["controller"].ToString();
    var action = Request.RequestContext.RouteData.Values["action"].ToString();
    string cssClass = "active";
    bool isActive = false;
}
<nav class="navbar-default navbar-static-side" role="navigation" style="display:none;">
    <div class="sidebar-collapse">
        <ul class="nav" id="side-menu">
            <li class="nav-header">
                <div class="logo-element">

                </div>
            </li>
            @foreach (var item in Model.Where(p => p.ParentId == null).OrderBy(p => p.OrderNo))
            {
                var children = Model.Where(p => p.ParentId == item.Id).OrderBy(p => p.OrderNo).ToList();
                var lv1Active = false;
                if (!isActive)
                {
                    lv1Active = children.Any(p => (p.Controller ?? "").ToLower() == controller.ToLower());
                    if (lv1Active)
                    {
                        isActive = true;
                    }
                }
                <li class="@(lv1Active?cssClass:"")">
                    <a>
                        @if (!string.IsNullOrWhiteSpace(item.IconCls))
                        {
                            <i class="@item.IconCls"></i>
                        }
                        <span class="nav-label">@item.Title</span> <span class="fa arrow"></span>
                    </a>
                    <ul class="nav nav-second-level">
                        @foreach (var childItem in children)
                        {
                            var lv2Active = false;
                            if (lv1Active && !string.IsNullOrWhiteSpace(childItem.Url))
                            {
                                lv2Active = (childItem.Controller ?? "").ToLower() == controller.ToLower() && Request.Url.AbsoluteUri.IndexOf(childItem.Url, StringComparison.CurrentCultureIgnoreCase) != -1;
                            }
                            <li class="@(lv2Active?cssClass:"")">
                                <a href="@childItem.Url">
                                    @if (!string.IsNullOrWhiteSpace(childItem.IconCls))
                                    {
                                        <i class="@childItem.IconCls"></i>
                                    }
                                    @childItem.Title
                                </a>
                            </li>
                        }

                    </ul>
                </li>
            }
        </ul>

    </div>
</nav>